/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tcloudclient.fileSystemInfo;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.Icon;
import javax.swing.table.AbstractTableModel;
import tcloudclient.fileSystem.FileNode;

/**
 *
 * @author Tomas
 */
public class FileSystemTableModel extends AbstractTableModel {

    public static final Double ZERO = new Double(0);
    private String[] columnNames = {"", "Název", "Typ", "Stav", "Velikost", "Poslední změna"};
    private DateFormat dateFormat = new SimpleDateFormat("HH:mm dd.MM.yyyy");
    private FileNode parent;

    public FileSystemTableModel(FileNode parent) {
        this.parent = parent;
    }
    
    @Override
    public int getRowCount() {
        return parent.getChilds().size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        FileNode get = parent.getChilds().get(rowIndex);
        switch (columnIndex) {
            case 0:
                return IconUtil.getIcon(get.getFile());
            case 1:
                return get.getName();
            case 2:
                return get.isLeaf() ? "Soubor" : "Složka";
            case 3:
                return get.getStatus();
            case 4:
                if(get.isLeaf()){
                    Long vel = get.getFile().length() / 1000;
                    if(vel < 1){
                        vel = 1l;
                    }
                    return vel + " KB";
                }else{
                    return "0 KB";
                }
            case 5:
                return dateFormat.format(new Date(get.getLastChange()));
            default:
                return "";
        }
    }

    public void setParent(FileNode parent) {
        this.parent = parent;
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        if(columnIndex == 0){
            return Icon.class;
        }
        return String.class;
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }
    
    
}
